package com.card.hwatt.assembler;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.card.hwatt.core.ObjectUtil;
import com.card.hwatt.dto.HolidayMonth;
import com.card.hwatt.dto.HolidayYear;
import com.card.hwatt.entity.*;

/**
 * 节假日
 * @author Administrator
 *
 */
public final class HolidayAssembler {

	public static List<com.card.hwatt.dto.Holiday> convert(List<Holiday> list) {
		List<com.card.hwatt.dto.Holiday> rows = new ArrayList<com.card.hwatt.dto.Holiday>();
		for (Holiday item : list) {
			com.card.hwatt.dto.Holiday dto = convert(item);
			rows.add(dto);
		}
		return rows;
	}

	public static com.card.hwatt.dto.Holiday convert(Holiday entity) {
		com.card.hwatt.dto.Holiday dto = new com.card.hwatt.dto.Holiday();
		ObjectUtil.fieldCopy(entity, dto);

		return dto;
	}

	public static Holiday convert(com.card.hwatt.dto.Holiday dto) {
		Holiday entity = new Holiday();
		ObjectUtil.fieldCopy(dto, entity);
		return entity;
	}

	public static HolidayYear create(int year) {
		Calendar calendarStart = Calendar.getInstance();
		calendarStart.set(year, 0, 1);
		Calendar calendarEnd = Calendar.getInstance();
		calendarEnd.set(year + 1, 0, 1);

		HolidayYear holidayYear = new HolidayYear();
		holidayYear.months = new ArrayList<HolidayMonth>();

		for (; calendarStart.before(calendarEnd); calendarStart.add(
				Calendar.DATE, 1)) {
			int month = calendarStart.get(Calendar.MONTH) + 1;
			HolidayMonth holidayMonth = null;
			for (HolidayMonth item : holidayYear.months) {
				if (item.month == month) {
					holidayMonth = item;
				}
			}

			if (holidayMonth == null) {
				holidayMonth = new HolidayMonth();
				holidayMonth.month = month;
				holidayYear.months.add(holidayMonth);
			}

			Class<HolidayMonth> cls = HolidayMonth.class;
			try {
				int day = calendarStart.get(Calendar.DATE);
				Field filed = cls.getField("d" + day);
				int week = calendarStart.get(Calendar.DAY_OF_WEEK);
				boolean isHolidayDate = week == Calendar.SATURDAY
						|| week == Calendar.SUNDAY;

				filed.set(holidayMonth, isHolidayDate ? "休" : "工");
				if (isHolidayDate) {
					holidayMonth.holidays++;
					holidayYear.holidays++;
				} else {
					holidayMonth.workdays++;
					holidayYear.workdays++;
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return holidayYear;
	}

	public static List<HolidayYear> convertHolidayYearList(List<Holiday> holidaies) {

		List<com.card.hwatt.dto.Holiday> list = convert(holidaies);
		List<HolidayYear> result = new ArrayList<HolidayYear>();

		for (com.card.hwatt.dto.Holiday holiday : list) {
			if (holiday.id.length() < 4) {
				continue;
			}
			int year = Integer.valueOf(holiday.id.substring(0, 4));
			HolidayYear holidayYear = null;
			for (HolidayYear h : result) {
				if (h.year == year) {
					holidayYear = h;
					break;
				}
			}
			if (holidayYear == null) {
				holidayYear = new HolidayYear();
				holidayYear.year = Integer.valueOf(year);
				holidayYear.months = new ArrayList<HolidayMonth>();
				result.add(holidayYear);
			}

			int month = Integer.valueOf(holiday.id.substring(4, 6));
			HolidayMonth holidayMonth = null;
			for (HolidayMonth m : holidayYear.months) {
				if (m.month == month) {
					holidayMonth = m;
					break;
				}
			}

			if (holidayMonth == null) {
				holidayMonth = new HolidayMonth();
				holidayMonth.month = month;
				holidayYear.months.add(holidayMonth);
			}

			Class<HolidayMonth> cls = HolidayMonth.class;
			int day = Integer.valueOf(holiday.id.substring(6, 8));
			try {
				Field filed = cls.getField("d" + day);
				filed.set(holidayMonth, "休");
				holidayYear.holidays++;
				holidayMonth.holidays++;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		for (HolidayYear holidayYear : result) {
			Calendar calendarStart = Calendar.getInstance();
			calendarStart.set(holidayYear.year, 0, 1);
			Calendar calendarEnd = Calendar.getInstance();
			calendarEnd.set(holidayYear.year + 1, 0, 1);

			for (; calendarStart.before(calendarEnd); calendarStart.add(
					Calendar.DATE, 1)) {
				int month = calendarStart.get(Calendar.MONTH) + 1;
				HolidayMonth holidayMonth = null;
				for (HolidayMonth item : holidayYear.months) {
					if (item.month == month) {
						holidayMonth = item;
					}
				}

				if (holidayMonth == null) {
					holidayMonth = new HolidayMonth();
					holidayMonth.month = month;
					holidayYear.months.add(holidayMonth);
				}

				Class<HolidayMonth> cls = HolidayMonth.class;
				try {
					int day = calendarStart.get(Calendar.DATE);
					Field filed = cls.getField("d" + day);
					if (filed.get(holidayMonth) == null) {
						filed.set(holidayMonth, "工");
						holidayMonth.workdays++;
						holidayYear.workdays++;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		return result;
	}
}
